package at.fhj.swd.pse.domain;

import static org.junit.Assert.*;

import java.util.List;

import javax.persistence.EntityNotFoundException;

import org.junit.Before;
import org.junit.Test;

import at.fhj.swd.pse.data.ActivityDAO;
import at.fhj.swd.pse.data.UserDAOMock;
import at.fhj.swd.pse.domain.UserService;
import at.fhj.swd.pse.domain.UserServiceImpl;
import at.fhj.swd.pse.JpaTestBase;
import at.fhj.swd.pse.entities.Activity;
import at.fhj.swd.pse.entities.Community;
import at.fhj.swd.pse.entities.User;

/**
 * @author Florian Maderbacher
 */

public class UTUserService
	extends JpaTestBase {

	UserService m_service;
	UserDAOMock m_dao;
	ActivityServiceStub m_as;
	
    private User createNewUser(String username, String password){
    	User user = new User();
		user.setFirstName("A");
		user.setLastName("User");
		user.setAddress("Anyroad");
		user.setCity("Nowhere");
		user.setEmail("a.user@somenet.com");
		user.setUsername(username);
		user.setPassword(password);
		return user;
    }
	
	private final class ActivityServiceStub implements ActivityService {
		@Override
		public List<Activity> getAllActivities(int userId) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Activity createNewActivity(Activity activity) {
			return null;
		}

		@Override
		public Activity createNewActivity(Activity activity, Community community) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void removeActivity(Activity activity) {
			// TODO Auto-generated method stub
		}

		@Override
		public void setA_dao(ActivityDAO a_dao) {
			// TODO Auto-generated method stub
			
		}
	}
    
	@Before
    public void setUp()
    {
		m_service = new UserServiceImpl();
		m_dao = new UserDAOMock();
		m_as = new ActivityServiceStub();
		m_service.setUserDAO(m_dao);
		m_service.setAs(m_as);
    }   

	
	@Test
	public void testLogin(){
		//Setup
		//myPassword -> 5413ee24723bba2c5a6ba2d0196c78b3ee4628d1
		User existingUser = createNewUser("myUsername","5413ee24723bba2c5a6ba2d0196c78b3ee4628d1");
		m_dao.insert(existingUser);
		
		//Test
		User t_user = m_service.login("myUsername", "myPassword");
		assertEquals(existingUser,t_user);
	}
	
	@Test
	public void testLoginWithoutHash(){
		//Setup
		//myPassword -> 5413ee24723bba2c5a6ba2d0196c78b3ee4628d1
		User existingUser = createNewUser("myUsername","myPassword");
		m_dao.insert(existingUser);
		
		//Test
		User t_user = m_service.login("myUsername", "myPassword");
		assertNull(t_user);
	}
	
	@Test
	public void testLoginEmptyPassword(){
		//Setup
		//myPassword -> 5413ee24723bba2c5a6ba2d0196c78b3ee4628d1
		User existingUser = createNewUser("myUsername","5413ee24723bba2c5a6ba2d0196c78b3ee4628d1");
		m_dao.insert(existingUser);
		
		//Test
		User t_user = m_service.login("myUsername", "");
		assertNull(t_user);
	}
	
	@Test
	public void testLoginPasswordNull(){
		//Setup
		//myPassword -> 5413ee24723bba2c5a6ba2d0196c78b3ee4628d1
		User existingUser = createNewUser("myUsername","5413ee24723bba2c5a6ba2d0196c78b3ee4628d1");
		m_dao.insert(existingUser);
		
		//Test
		User t_user = m_service.login("myUsername", null);
		assertNull(t_user);
	}
	
	@Test
	public void addFriends(){
		User user = createNewUser("user","5413ee24723bba2c5a6ba2d0196c78b3ee4628d1");
		User friend = createNewUser("user","5413ee24723bba2c5a6ba2d0196c78b3ee4628d1");
		m_service.addFriend(user, friend);
		assertFalse(user.getFriends().isEmpty());
		assertFalse(friend.getFriends().isEmpty());
		assertTrue(user.getFriends().contains(friend));
		assertTrue(friend.getFriends().contains(user));
	}
	
	@Test
	public void removeFriends(){
		User user = createNewUser("user","5413ee24723bba2c5a6ba2d0196c78b3ee4628d1");
		User friend = createNewUser("user","5413ee24723bba2c5a6ba2d0196c78b3ee4628d1");
		m_service.addFriend(user, friend);
		m_service.removeFriend(user, friend);
		assertTrue(user.getFriends().isEmpty());
		assertTrue(friend.getFriends().isEmpty());
	}
	
	@Test 
	public void getAllUsers(){
		User user = createNewUser("user","5413ee24723bba2c5a6ba2d0196c78b3ee4628d1");
		User user2 = createNewUser("user2","5413ee24723bba2c5a6ba2d0196c78b3ee4628d1");
		m_dao.insert(user);
		m_dao.insert(user2);
		assertFalse(m_service.getUsers().isEmpty());
		assertTrue(m_service.getUsers().contains(user));
		assertTrue(m_service.getUsers().contains(user2));
	}
	
	@Test
	public void updateUser(){
		User user = createNewUser("user","5413ee24723bba2c5a6ba2d0196c78b3ee4628d1");
		m_dao.insert(user);
		user.setUsername("Test");
		m_service.updateUser(user);
		assertTrue(m_dao.getAll().contains(user));

	}
	
	@Test
	public void getUserByID(){
		User user = createNewUser("user","5413ee24723bba2c5a6ba2d0196c78b3ee4628d1");
		user.setId(1);
		m_dao.insert(user);
		assertEquals(m_service.getUserProfileByUserId(1), user);
	}
	
	@Test(expected=EntityNotFoundException.class)
	public void getUserByWrongId(){
		User user = createNewUser("user","5413ee24723bba2c5a6ba2d0196c78b3ee4628d1");
		user.setId(1);
		m_dao.insert(user);
		m_service.getUserProfileByUserId(2);
	}
}
